package ui.graphical.afbeelding;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ScaleImage {
	/**
	 * Bepaal de formatName aan de hand van de extensie
	 * @param fileName
	 */
	private static String getFormatName(String fileName) {
		return fileName.substring(fileName.lastIndexOf(".")+1).toUpperCase();
	}
	// bron: http://www.rgagnon.com/javadetails/java-0243.html
	public static void scale(String src, int width, int height, String dest) throws IOException {
		BufferedImage bsrc = ImageIO.read(new File(src));
		BufferedImage bdest = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics2D g = bdest.createGraphics();
		AffineTransform at = AffineTransform.getScaleInstance((double) width
				/ bsrc.getWidth(), (double) height / bsrc.getHeight());
		g.drawRenderedImage(bsrc, at);
		ImageIO.write(bdest, getFormatName(dest), new File(dest));
	}
}
